#!/usr/bin/python

import wx
import wx.grid
import lotto_db

# begin wxGlade: extracode
# end wxGlade


class NumberStats(wx.Dialog):
    def __init__(self, *args, **kwds):
        # begin wxGlade: NumberStats.__init__
        kwds["style"] = wx.DEFAULT_DIALOG_STYLE
        wx.Dialog.__init__(self, *args, **kwds)
        self.spin_ctrl_1 = wx.SpinCtrl(self, -1, "0", min=1, max=60)
        self.stats = wx.grid.Grid(self)
        self.button_1 = wx.Button(self, wx.ID_OK, "Done")

        self.__set_properties()
        self.__do_layout()
        # end wxGlade
        self.dbconn = lotto_db.lotto_db()

    def __set_properties(self):
        # begin wxGlade: NumberStats.__set_properties
        self.SetTitle("Statistics")
        self.stats.CreateGrid(10, 4)
        # end wxGlade
        self.stats.SetColLabelValue(0, "Skip \ncount")
        self.stats.SetColLabelValue(1, "Skip \ntotal")
        self.stats.SetColLabelValue(2, "Skip \npattern")
        self.stats.SetColLabelValue(3, "Last \ndraw dates")
        self.Bind(wx.EVT_SPINCTRL, self.OnSpinChange, self.spin_ctrl_1)

    def __do_layout(self):
        # begin wxGlade: NumberStats.__do_layout
        sizer_1 = wx.BoxSizer(wx.VERTICAL)
        sizer_1.Add(self.spin_ctrl_1, 0,
             wx.ALIGN_CENTER_HORIZONTAL | wx.ADJUST_MINSIZE, 0)
        sizer_1.Add(self.stats, 1, wx.EXPAND, 1)
        sizer_1.Add(self.button_1,
             0, wx.ALIGN_CENTER_HORIZONTAL | wx.ADJUST_MINSIZE, 0)
        self.SetSizer(sizer_1)
        sizer_1.Fit(self)
        self.Layout()
        # end wxGlade

    def OnSpinChange(self, event):
        #get number from spinner
        check_number = self.spin_ctrl_1.GetValue()
        # order=2 descending will put highest skip count first
        number_table = self.dbconn.get_sorted_skip(check_number, 2)
        draw_table = self.dbconn.get_table("Whiteball_" + str(check_number))
        #fill dialog will stats
        for i in range(0, 11):
            self.stats.SetCellValue(i, 0, str(number_table[i][0]))
            self.stats.SetCellValue(i, 1, str(number_table[i][1]))
            date_index = draw_table[i]
            self.stats.SetCellValue(i, 3, str(
                self.dbconn.get_draw_date(date_index[0])))

# end of class NumberStats

if __name__ == "__main__":
    app = wx.PySimpleApp(0)
    wx.InitAllImageHandlers()
    dialog_1 = NumberStats(None, -1, "")
    app.SetTopWindow(dialog_1)
    dialog_1.Show()
    app.MainLoop()
